447C - DZY Loves Sequences - CodeForces Solution


dp *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_multiset;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;


#pragma GCC optimize("Ofast")  
//#pragma GCC target("avx,avx2,fma") 
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("-O2")

#define all(a)             (a).begin(), (a).end()
#define ll                 long long
#define fi                 first
#define se                 second
#define pb                 push_back
#define pob                pop_back
#define bb                 begin()
#define ee                 end()
#define endll              '\n'

#define pii             pair<int,int>
#define pll             pair<long long int,long long int>
#define vi              vector<int>
#define vll             vector<long long int>
#define vii             vector<pii>
#define vpll            vector<pll>
 
#define inf               (1ll<<30ll)-1ll
#define llinf             (1ll<<62ll)-1ll

#define mod                1000000007
#define n_size             200005
#define g_size             20000005
#define eps                1e-9

const double PI=acos(-1);

//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 way Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; //Hexagonal Direction

vi v;

inline void solve(){

    int n,m,t,res;

    int l,r,mid;

    int maxi=-inf,mini=inf,count=0;

    int i,j,k;

    bool flag;

    vi prefix,suffix;

    cin>>n;
    v.assign(n+2,0);
    prefix.assign(n+2,0);
    suffix.assign(n+2,0);

    v[0]=-inf;
    v[n+1]=inf;

    for(i=1;i<=n;i++) cin>>v[i];

    if(n==1){
        cout<<1<<endl;
        return;
    }

    for(i=1;i<=n;i++){
        if(v[i-1]<v[i]) prefix[i]=prefix[i-1]+1;
        else prefix[i]=1; 
    }

    suffix[n]=1;
    for(i=n-1;i>=1;i--){
        if(v[i]<v[i+1]) suffix[i]=suffix[i+1]+1;
        else suffix[i]=1;
    }

    for(i=1;i<n;i++){
        if(v[i+1]-v[i-1]>=2){
            maxi=max(maxi,prefix[i-1]+suffix[i+1]+1);
        }
        else maxi=max(maxi,prefix[i]+1);
    }

    for(i=n;i>=2;i--){
        maxi=max(maxi,suffix[i]+1);
    }

    // for(i=0;i<=n+1;i++) cout<<prefix[i]<<" ";
    // cout<<endl;
    // for(i=0;i<=n+1;i++) cout<<suffix[i]<<" ";
    // cout<<endl;

    cout<<maxi<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
    //cout<<fixed<<setprecision(11);


    int T=1;
    //cin>>T;

    while(T--) solve();
    
    
}


Comments

Submit
0 Comments
More Questions

510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops